/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pokemon.services;

import com.pokemon.dao.BadgeDAO;
import com.pokemon.dto.BadgeDTO;
import com.pokemon.dto.DTOConvertor;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Branislav Paulis
 */
@Service("badgeManager")
@Transactional
public class BadgeServiceImpl implements BadgeService {

    @Autowired
    private BadgeDAO badgeDAO;
   
    @Required
    @Autowired
    public void setBadgeDAO(BadgeDAO badgeDAO)
    {
        this.badgeDAO = badgeDAO;
    }

    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_USER')")
    @Transactional
    public BadgeDTO addBadge(BadgeDTO badge) {
        badgeDAO.addBadge(DTOConvertor.dtoToEntity(badge));
        return badge;
    }

    @Transactional
    public BadgeDTO getBadge(Long id) {
        return DTOConvertor.entityToDto(badgeDAO.getBadge(id));
    }
   
    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @Transactional
    public BadgeDTO updateBadge(BadgeDTO badge) {
        badgeDAO.updateBadge(DTOConvertor.dtoToEntity(badge));
        return badge;
    }

    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @Transactional
    public BadgeDTO removeBadge(BadgeDTO badge) {
        badgeDAO.removeBadge(DTOConvertor.dtoToEntity(badge));
        return badge;
    }

    @Transactional
    public List<BadgeDTO> getAllBadges() {
        return DTOConvertor.badgeEntityListToDtoList(badgeDAO.getAllBadges());
    }
}
